<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="listQuery.name" placeholder="名称" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
        搜索
      </el-button>
    </div>
    <el-table
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%"
      @selection-change="handleSelectionChange"
      ref="multipleTable"
    >
      <el-table-column
        type="selection"
        width="55">
      </el-table-column>
      <el-table-column align="center" label="封面" width="180">
        <template slot-scope="{row}">
          <el-image :src="row.cover" fit="scale-down" style="width: 120px;height: 50px" :preview-src-list="[row.cover]">
            <div slot="error" class="image-slot">
              <i class="el-icon-picture-outline" style="font-size: 40px"></i>
            </div>
          </el-image>
        </template>
      </el-table-column>
      <el-table-column align="center" label="名称">
        <template slot-scope="{row}">
          <span>{{ row.name }}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="操作" width="120">
        x
      </el-table-column>
    </el-table>
    <div style="position: relative">
      <el-button v-waves type="primary" style="position: absolute;right: 0px" @click="handleDefiniteSelection">
        确定选择
      </el-button>
    </div>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.per_page" @pagination="onList" />
  </div>
</template>

<script>
import { getList } from '@/api/feature'
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination

export default {
  name: 'FeatureDialog',
  components: { Pagination },
  data() {
    return {
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        per_page: 10,
        name: undefined
      },
      multipleSelection: []
    }
  },
  created() {
    this.onList()
  },
  methods: {
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    handleDefiniteSelection() {
      if (!this.multipleSelection.length) {
        this.$notify({
          title: '警告',
          message: '请选择功能',
          type: 'warning'
        })
        return
      }
      console.log(this.multipleSelection, 'this.multipleSelection')
      const select = this.multipleSelection

      this.$emit('clickSelectFeature', { select })
      this.multipleSelection = []
      this.$refs.multipleTable.clearSelection()
    },
    onList() {
      this.listLoading = true
      getList(this.listQuery).then(response => {
        this.list = response.data.data
        this.total = response.data.total
        this.listLoading = false
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.onList()
    }
  }
}
</script>

<style scoped>
  .edit-input {
    padding-right: 100px;
  }
  .cancel-btn {
    position: absolute;
    right: 15px;
    top: 10px;
  }
</style>
